package com.example.funny1024;

import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

import java.util.Arrays;

public class Permission extends AceAbility {
    @Override
    public void onStart(Intent intent) {
        setInstanceName("default");
        setPageParams("pages/black/black", null);
        super.onStart(intent);
        if(verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) == IBundleManager.PERMISSION_DENIED){
            requestPermissionsFromUser(new String[]{SystemPermission.DISTRIBUTED_DATASYNC}, 0);
        }else{
            Intent intentData = new Intent();
            intentData.setParam("res", true);
            setResult(0, intentData);
            terminateAbility();
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        Intent intentData = new Intent();
        intentData.setParam("res", Arrays.stream(grantResults).sum() == 0);
        setResult(0, intentData);
        terminateAbility();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}
